<?php

require_once(dirname(__FILE__).'/Constants.php');
require_once(dirname(__FILE__).'/Config.php');
require_once(dirname(__FILE__).'/Util.php');
require_once(dirname(__FILE__).'/MixiSignatureMethod.php');

class MixiAppliAPI {
    public $owner_id;
    public $viewer_id;
    public $params;
    public $result;

    private $return_code   = ERR_NO_ERROR;
    private $error_message = '';

    public function __construct ($params = array()) {
        $this->params = $params;

        if (!IS_DEVEL && $this->validate_oauth_token() == false) {
            $this->return_code   = ERR_INVALID_OAUTH_TOKEN;
            $this->error_message = 'invalid oauth token';
            $this->output();
        }

        $owner_id  = $params['opensocial_owner_id'];
        $viewer_id = $params['opensocial_viewer_id'];

        if (!$owner_id || !is_numeric($owner_id)) {
            $this->return_code   = ERR_INVALID_OWNER_ID;
            $this->error_message = 'invalid owner_id';
            $this->output();
        }
        if (!$viewer_id || !is_numeric($viewer_id)) {
            $this->return_code   = ERR_INVALID_VIEWER_ID;
            $this->error_message = 'invalid viewer_id';
            $this->output();
        }

        $this->owner_id  = $owner_id;
        $this->viewer_id = $viewer_id;

        return true;
    }

    function validate_oauth_token () {
        $request = OAuthRequest::from_request(null, null, $this->params);
        $signature_method = new MixiSignatureMethod();
        @$signature_valid = $signature_method->check_signature($request, null, null, $this->params["oauth_signature"]);
        return $signature_valid;
    }

    function validate_params ($validate_rules) {
        $invalid = '';
        foreach ($validate_rules as $key => $per_param_rules) {
            foreach ($per_param_rules as $rule) {
                if (is_array($rule)) {
                    $args = array_splice($rule, 1);
                    $rule = $rule[0];
                }
                switch ($rule) {
                    case 'not_blank':
                        if (!isset($this->params[$key]) || $this->params[$key] == '')
                            $invalid = "$key@$rule";
                        break;
                    case 'not_space_only':
                        if (isset($this->params[$key]) && preg_match('/^\s+$/', $this->params[$key]))
                            $invalid = "$key@$rule";
                        break;
                    case 'is_numeric':
                        if (isset($this->params[$key]) && (
                            !is_numeric($this->params[$key])
                        )) {
                            $invalid = "$key@$rule";
                        }
                        break;
                    case 'length':
                        if (isset($this->params[$key]) && strlen($this->params[$key]) != $args[0])
                            $invalid = "$key@$rule";
                        break;
                    case 'date':
                        list($y, $m, $d) = explode('-', $this->params[$key]);
                        if (isset($this->params[$key]) && !is_date($this->params[$key]))
                            $invalid = "$key@$rule";
                        break;
                    case 'time':
                        list($h, $i, $s) = explode(':', $this->params[$key]);
                        if (isset($this->params[$key]) && !check_time($h, $i, $s))
                            $invalid = "$key@$rule";
                        break;
                    case 'in_array':
                        if (isset($this->params[$key]) && !in_array($this->params[$key], $args))
                            $invalid = "$key@$rule";
                        break;
                    default:
                        break;
                }
            }
        }
        if ($invalid) {
            $this->return_code   = ERR_INVALID_PARAMS;
            $this->error_message = 'invalid param :'.$invalid;
            $this->output();
        }
        return true;
    }

    function set_error ($error_message = '') {
        $this->return_code   = ERR_PROCESS_ERROR;
        $this->error_message = $error_message;
        $this->output();
    }

    function set_result ($result) {
        $this->result = $result;
        $this->output();
    }

    private function output () {
        if (!IS_DEVEL) header('Content-Type: application/json; charset=utf8');
        $response = array(
            'return_code'   => $this->return_code,
            'error_message' => $this->error_message,
        );
        if (isset($this->result))
            $response['result'] = $this->result;

        $response['debug'] = $this->params;

        print json_encode($response);
        exit(0);
    }

}
